/* 
 * File:   main.cpp
 * Author: grm
 *
 * Created on 21 février 2011, 22:58
 */

#include <cstdlib>
#include "decode.h"
#include "wrapper.h"

#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <omp.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

/*
 * 
 */
static int file_filter(const dirent *filename) {
    const char *extension;
    extension = strrchr(filename->d_name, '.');


    if (extension == NULL) {
        return 0;
    }

    extension++;

    /* return only known asset types */
    if (strncasecmp(extension, "j2c", 3) != 0 &&
            strncasecmp(extension, "j2k", 3) != 0 &&
            strncasecmp(extension, "wav", 3) != 0) {
        return 0;
    }

    return 1;
}

int get_filelist(context_t *context, char *in_path, filelist_t *filelist) {
    struct dirent **files;
    struct stat st_in;
    struct stat st_out;
    int x = 0;

    if (stat(in_path, &st_in) != 0) {
        printf("Could not open input file %s", in_path);
        return -1;
    }

    if (S_ISDIR(st_in.st_mode)) {
        /* if input is directory, it is jpeg2000 or pcm */
        filelist->count = scandir(in_path, &files, file_filter, alphasort);
        filelist->files = (char**) malloc(filelist->count * sizeof (char*));
        if (filelist->count) {
            for (x = 0; x < filelist->count; x++) {
                filelist->files[x] = (char *) malloc(MAX_FILENAME_LENGTH);
                sprintf(filelist->files[x], "%s/%s", in_path, files[x]->d_name);
            }
        }
    } else {
        /* mpeg2 or time_text */
        int essence_type = get_file_essence_type(in_path);
        if (essence_type == AET_UNKNOWN) {
            return ENCTOOLS_ERROR;
        }
        filelist->count = 1;
        filelist->files = (char**) malloc(filelist->count * sizeof (char*));
        filelist->files[0] = (char *) malloc(MAX_FILENAME_LENGTH);
        filelist->files[0] = in_path;
    }

    return ENCTOOLS_SUCCESS;
}

static int tif_file_filter(const dirent *filename) {
    const char *extension;
    extension = strrchr(filename->d_name, '.');


    if (extension == NULL) {
        return 0;
    }

    extension++;

    if (strncasecmp(extension, "tiff", 4) != 0) {
        return 0;
    }

    return 1;
}

int get_tif_filelist(char *in_path, filelist_t *filelist) {
    struct dirent **files;
    struct stat st_in;
    struct stat st_out;
    int x = 0;

    if (stat(in_path, &st_in) != 0) {
        printf("Could not open input file %s", in_path);
        return -1;
    }

    if (S_ISDIR(st_in.st_mode)) {
        /* if input is directory, it is jpeg2000 or pcm */
        filelist->count = scandir(in_path, &files, tif_file_filter, alphasort);
        filelist->files = (char**) malloc(filelist->count * sizeof (char*));
        if (filelist->count) {
            for (x = 0; x < filelist->count; x++) {
                filelist->files[x] = (char *) malloc(MAX_FILENAME_LENGTH);
                sprintf(filelist->files[x], "%s/%s", in_path, files[x]->d_name);
            }
        }
    }

    return ENCTOOLS_SUCCESS;
}

int main(int argc, char** argv) {

    decode_only("/home/encode/big_buck_bunny_1080p_surround.avi", "/home/encode/tiff/", "tiff");
    //    decode_only("/home/grm/dev/Jpeg2000Encoder/lenna.mpg", "/home/grm/dev/Jpeg2000Encoder/out/", "tiff");
    filelist_t *tiff_list = (filelist_t *) malloc(sizeof (filelist_t));
    memset(tiff_list, 0, sizeof (filelist_t));
    get_tif_filelist("/home/encode/tiff/", tiff_list);

    int ii = 0;
    //XXX: constant

#pragma omp parallel for 
    for (ii = 0; ii < tiff_list->count; ii++) {
        char* frameName = (char*) malloc(100 * sizeof (char));
        sprintf(frameName, "/home/encode/j2c/frame%09d.j2c", ii + 1);
        //        encode_from_file(tiff_list->files[ii], frameName, "", 0, "tiff");

        encode_main(tiff_list->files[ii], frameName);
        free(frameName);
    }


    //    decode_encode_local("/home/grm/dev/distJpeg/src/vid/big_buck_bunny_1080p_h264.mov", "/mnt/floppy/tmp/", 2);
    //    decode_encode_local("/home/grm/dev/openjpeg/lenna.mpg", "/home/grm/dev/openjpeg/out/", 2);
    context_t *context;
    filelist_t *filelist;
    char out_file[4096];

    context = (context_t *) malloc(sizeof (context_t));
    memset(context, 0, sizeof (context_t));

    filelist = (filelist_t *) malloc(sizeof (filelist_t));
    memset(filelist, 0, sizeof (filelist_t));

    context->xyz = 1;
    context->log_level = LOG_WARN;
    context->ns = XML_NS_SMPTE;
    context->frame_rate = 24;
    context->threads = 4;
    context->ns = XML_NS_SMPTE; //context->ns = XML_NS_INTEROP;


    get_filelist(context, "/home/encode/j2c/", filelist);
    if (filelist->count < 1) {
        printf("No input files located");
        exit(1);
    }

    if (write_mxf(context, filelist, "/home/encode/mxf/out.mxf") != 0) {
        printf("Error!\n");
    }

    if (context != NULL) {
        free(context);
    }

    if (filelist != NULL) {
        free(filelist);
    }

    if (context->log_level > 0) {
        printf("\n");
    }

    exit(0);
    //    int write_mxf(context_t *context, filelist_t *filelist, char *output_file);
}